package day9zy.zixun;

import java.io.*;
import java.net.*;
public class ZixunServer {
    private int port=8000;
    private DatagramSocket socket;

    public ZixunServer() throws IOException {
        socket=new DatagramSocket(port);
        System.out.println("物流服务器启动");
    }

    public String echo(String msg) {
        return msg;
    }

    public void service() {
        while (true) {
            try {
                DatagramPacket packet=new DatagramPacket(new byte[512],512);
                socket.receive(packet);
                //getLength是实际读取的byte长度
                String msg=new String(packet.getData(),0,packet.getLength());
                System.out.println(packet.getAddress() + ":" +packet.getPort()
                        +">"+msg);

                packet.setData(echo(msg).getBytes());
                socket.send(packet);
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[])throws IOException {
        new ZixunServer().service();
    }
}

